<?php


namespace app\web\controller;


use app\api\controller\v2\Auth;
use app\common\lib\exception\ApiException;
use app\common\lib\Util;
use think\Controller;
use think\Exception;
use think\Log;

class NatalReport extends Controller
{
    protected $type = [
        39 => 'feelings', // 恋爱
        40 => 'dating', // 婚姻
        41 => 'money', // 财富
        42 => 'carrer', // 事业
        52 => 'academic', // 学业
    ];
    protected $recodeInfo = [];


    public function _initialize()
    {
        $res = Util::androidVersion('2.2.0');
        $this->assign('version', $res);
    }

    public function index()
    {
        $height = input('h/d', 0);
        $gid = input('gid/d', 0);
        $rid = input('rid/d', 0);
        $orderId = input('order_id/s', '');
        // 上线必须修改成false，关闭测试模式，测试模式为实时计算，页面无权限验证
        $isTest = false;
        $userInfo = null;

        if (!isset($this->type[$gid]))
        {
            Log::write(date('Y-m-d H:i:s') . 'gid参数错误:' . $gid, 'error', true);
            throw new ApiException('参数错误');
        }
        $seifRid = 0;
        // && $gid !== 52 && $gid !== 40
        if (!$isTest)
        {
            //校验token
            try
            {
                $auth = new Auth();
                $auth->checkRequestAuth();
                $userInfo = $auth->user;
                $seifRid = (int)$userInfo['rid'];
            }
            catch (Exception $e)
            {
                throw new ApiException('一生报告，错误：' . '权限认证失败,请稍后重试！');
            }
        }

        $appType = $this->request->header('app-type');

        $uid = $userInfo ? (int)$userInfo['id'] : 0;

        $natalReportData = (new \app\web\service\NatalReport())->getNatalReport($uid, $gid, $rid, $orderId, $isTest);

        $levels = $natalReportData['level'];
        $data = $natalReportData['data'];
        $text = $natalReportData['text'];


        $recodeIsChange = Util::CheckAppVersion('2.6.0') !== '小' ? $natalReportData['recode_is_change'] : false;
        $unlockRecodeInfo = [ // 解锁档案信息
            'recode_is_change' => $recodeIsChange,
            'info' => (new Util)->getFortuneDetailRecodeInfo([$natalReportData['recode_info']], $seifRid),
            'change_title' => $recodeIsChange ? '检测到档案信息发生修改' : '',
            'change_desc' => $recodeIsChange ? '已购买的运势内容不会随着档案的修改重新计算' : ''
        ];

        return $this->fetch($this->type[$gid], [
            'appType' => $appType,
            'height' => $height,
            'levels' => $levels,
            'text' => $text,
            'data' => $data,
            'is_show_nav' => 1,//是否展示导航栏
            'is_expand' => $unlockRecodeInfo['recode_is_change'] || $orderId,
            'unlock_recode_info' => $unlockRecodeInfo
        ]);
    }


}
